#include <iostream>
#include <cstring>
using namespace std;

class Cat
{
public:
    Cat(const char *name, int age, float weight)
    {
        this->name = new char[20];
        // 把实参拷贝到私有成员name中
        strcpy(this->name, name);

        this->age = age;
        this->weight = weight;
        cout << "有参构造函数" << endl;
    }
    Cat()
    {
        cout << "无参构造函数" << endl;
    }

    // --------------------这个----------------------------
    // 实现深拷贝
    Cat(const Cat &cat)
    {
        this->name = new char[20];
        strcpy(this->name, cat.name);
        this->age = cat.age;
        this->weight = cat.weight;
        cout << "拷贝构造函数" << endl;
    }

    void show()
    {
        cout << "name:" << name << endl;
        cout << "age:" << age << endl;
        cout << "weight:" << weight << endl;
        cout << "name的地址: " << (void *)name << endl;
        cout << "age的地址: " << &age << endl;
        cout << "weight的地址: " << &weight << endl;
    }

    void setAttr(const char *name, int age, float weight)
    {
        strcpy(this->name, name);
        this->age = age;
        this->weight = weight;
    }

private:
    char *name;
    int age;
    float weight;
};

int main()
{
    // 创建猫对象
    Cat c1("旺财", 5, 15.5);

    Cat c2 = c1; // 调用拷贝构造函数
    c1.show();
    c2.show();

    c1.setAttr("小黑", 3, 10.5);
    c1.show();
    c2.show();
    return 0;
}

